<script lang="ts" setup>
import type { DataType } from '../../types';

import { useVbenModal } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { deleteApps } from '#/api/admin/apps';

const name = ref('');
const [Form, formApi] = useVbenForm({
  handleSubmit: onSubmit,
  schema: [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      dependencies: {
        show: false,
        triggerFields: ['name'],
      },
      fieldName: 'name',
      label: '应用名',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'confirm_name',
      label: '应用名',
      rules: 'required',
    },
    {
      component: 'InputPassword',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'password',
      label: '网站创始人后台登录密码',
      rules: 'required',
    },
  ],
  showDefaultActions: false,
});
const [Modal, formModalApi] = useVbenModal({
  fullscreenButton: false,
  onConfirm: async () => {
    if (['demo'].includes(name.value)) {
      await formApi.validateAndSubmitForm();
    } else {
      formModalApi.close();
    }
  },
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const values: DataType = formModalApi.getData();
      if (values) {
        name.value = values.name;
        formApi.setFieldValue('name', name.value);
      }
    }
  },
});

async function onSubmit(values: any) {
  function showMsg(msg: string) {
    message.success(msg);
    formModalApi.setData({ isUpdata: true });
  }
  const { msg } = await deleteApps(values);

  showMsg(msg);
  formModalApi.close();
}
</script>
<template>
  <Modal class="w-3/5">
    <p class="mb-3 rounded-lg bg-amber-500 p-3 text-white">
      应用
      <code class="text-black">
        {{ name }}
      </code>
      的所有数据都将被删除，且无法恢复！
    </p>
    <template v-if="['demo'].includes(name)">
      <p class="mb-3 rounded-lg bg-amber-500 p-3 text-white">
        如果您确定要卸载应用
        <code class="text-black">
          {{ name }}
        </code>
        ，请填写如下信息：
      </p>
      <Form />
    </template>
    <template v-else>
      <p class="mb-3 rounded-lg bg-amber-500 p-3 text-white">
        您确定要卸载此应用
        <code class="text-black">
          {{ name }}
        </code>
        吗？<br />
      </p>

      <p class="mb-3 rounded-lg bg-amber-500 p-3 text-white">
        为了安全请在命令行执行下面命令：<br />
        <code class="text-black"> php think uninstall:app {{ name }} </code>
      </p>
    </template>
  </Modal>
</template>

<style lang="scss" scoped></style>
